#!/bin/bash
. lib/bsfl.sh
createconn_nc() {
URL=$1
if [[ -z $URL ]]; then
	echo "Must have an URL"
	display_status ERROR
	exit;
fi
    
ADDR_AUX=`echo $URL |cut -d / -f 3 -s`
PORT_AUX=`echo $URL |cut -d / -f 3|cut -d: -f2 -s`
PARAM_AUX=`echo $URL | cut -d \? -f2 -s`
HOST_AUX=`echo $ADDR_AUX|cut -d: -f1 `

ADDR=$ADDR_AUX
PORT=${PORT_AUX:=80}
HOST=$HOST_AUX
ADDR="$HOST:$PORT"

#echo "addr="$ADDR
#echo "url="$URL
#echo $ADDR $PORT $HOST 

METHOD=$2
if [[ -z $METHOD ]]; then
	METHOD="GET";
fi

POSTDATA=$3
CTLength=${#POSTDATA}
if [[ $CTLength -eq 0 ]]; then
	CTLength=19
fi

GETPARAM=$4
if [[ -z $GETPARAM ]]; then
	GETPARAM=$PARAM_AUX
fi

COOKIE=$5
if [[ -z $COOKIE ]]; then
	COOKIE=""
fi

UA=$6
if [[ -z $UA ]]; then
	UA="Scan"
fi

CONTENTTYPE=$7
if [[ -z $CONTENTTYPE ]]; then
	CONTENTTYPE="application/x-www-form-urlencoded"
	#CONTENTTYPE="text/html; charset=iso-8859-1"
fi

RESFILE=$8
if [[ -z $RESFILE ]]; then
	RESFILE=salida
fi

ENCODING=$9
if [[ -z $ENCODING ]]; then
	ENCODING=""
	#gzip,deflate
fi

REFERER=${10};
if [[ -z $REFERER ]]; then
	REFERER=$URL;
fi


echo 'nc  '$HOST' '$PORT' -q1 <<<'$METHOD' '$URL' HTTP/1.1
Host: '$ADDR'
User-Agent: '$UA'
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: es-es,es;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Accept-Encoding: '$ENCODING'
Keep-Alive: 115
Connection: keep-alive
Referer: '$REFERER'
Content-Length: '$CTLength'
Content-Type: '$CONTENTTYPE'
Cookie: '$COOKIE'

'$POSTDATA'

'>$RESFILE

nc  $HOST $PORT -q1 <<<$METHOD' '$URL' HTTP/1.1
Host: '$ADDR'
User-Agent: '$UA'
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: es-es,es;q=0.8,en-us;q=0.5,en;q=0.3
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Accept-Encoding: '$ENCODING'
Keep-Alive: 115
Connection: keep-alive
Referer: '$REFERER'
Content-Length: '$CTLength'
Content-Type: '$CONTENTTYPE'
Cookie: '$COOKIE'

'$POSTDATA'

'>>$RESFILE
echo DOWNLOAD DONE;display_status OK
}





createconn_curl() {
EXEC=$CURL
URL=$1
if [[ -z $URL ]]; then
	echo "Must have an URL"
	display_status ERROR
	exit;
fi    
ADDR_AUX=`echo $URL |cut -d / -f 3 -s`
PORT_AUX=`echo $URL |cut -d / -f 3|cut -d: -f2 -s`
PARAM_AUX=`echo $URL | cut -d \? -f2 -s`
HOST_AUX=`echo $ADDR_AUX|cut -d: -f1 `

ADDR=$ADDR_AUX
PORT=${PORT_AUX:=80}
HOST=$HOST_AUX
ADDR="$HOST:$PORT"

#echo "addr="$ADDR
#echo "url="$URL
#echo $ADDR $PORT $HOST 

METHOD=$2
if [[ -z $METHOD ]]; then
	METHOD="GET";
fi

POSTDATA=$3
CTLength=${#POSTDATA}
if [[ $CTLength -eq 0 ]]; then
	CTLength=19
fi

GETPARAM=$4
if [[ -z $GETPARAM ]]; then
	GETPARAM=$PARAM_AUX
fi

COOKIE=$5

UA=$6
if [[ -z $UA ]]; then
	UA="Scan"
fi

CONTENTTYPE=$7
if [[ -z $CONTENTTYPE ]]; then
	CONTENTTYPE="application/x-www-form-urlencoded"
fi

RESFILE=$8
if [[ -z $RESFILE ]]; then
	RESFILE=salida
fi

ENCODING=$9
if [[ -z $ENCODING ]]; then
	ENCODING=""
	#gzip,deflate
fi

REFERER=${10};
if [[ -z $REFERER ]]; then
	REFERER=$URL;
fi
EXEC="$EXEC -e $REFERER"


if [[ $METHOD = "POST" ]]; then
	EXEC="$EXEC -d $POSTDATA"
fi

EXEC="$EXEC -A $UA"
if [[ -n $COOKIE ]]; then
	EXEC="$EXEC -b $COOKIE"
fi

#EXEC="$EXEC -o $RESFILE "

EXEC="$EXEC $URL"
echo $EXEC >$RESFILE
$EXEC >> $RESFILE
echo DOWNLOAD DONE;display_status OK
}

createconn_wget() {
EXEC=$WGET
URL=$1
if [[ -z $URL ]]; then
	echo "Must have an URL"
	display_status ERROR
	exit;
fi    

METHOD=$2
if [[ -z $METHOD ]]; then
	METHOD="GET";
fi

POSTDATA=$3
CTLength=${#POSTDATA}
if [[ $CTLength -eq 0 ]]; then
	CTLength=19
fi

GETPARAM=$4
if [[ -z $GETPARAM ]]; then
	GETPARAM=$PARAM_AUX
fi

COOKIE=$5

UA=$6
if [[ -z $UA ]]; then
	UA="Scan"
fi

CONTENTTYPE=$7
if [[ -z $CONTENTTYPE ]]; then
	CONTENTTYPE="application/x-www-form-urlencoded"
fi

RESFILE=$8
if [[ -z $RESFILE ]]; then
	RESFILE=salida
fi

ENCODING=$9
if [[ -z $ENCODING ]]; then
	ENCODING=""
	#gzip,deflate
fi

REFERER=${10};
if [[ -z $REFERER ]]; then
	REFERER=$URL;
fi


EXEC="$EXEC --referer $REFERER"

if [[ $METHOD = "POST" ]]; then
	EXEC="$EXEC --post-data=$POSTDATA"
fi

EXEC="$EXEC --user-agent=$UA"
if [[ -n $COOKIE ]]; then
	EXEC="$EXEC --no-cookies --user-agent=Cookie: $COOKIE"
fi

#EXEC="$EXEC -o $RESFILE "

EXEC="$EXEC  $URL"
echo $EXEC >$RESFILE
$EXEC --output-document $RESFILE
echo DOWNLOAD DONE;display_status OK
}
